//
//  VVHomeToolBarView.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/31.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit

class VVHomeToolBarView: UIView {
    
    //转发
    var retweetButton: UIButton?
    
    //评论
    var commentButton: UIButton?
    
    //赞
    var unlikeButton: UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    var statusViewModel: VVStatusViewModel? {
        
        didSet {
            
            let count = statusViewModel?.status?.reposts_count ?? 0
            let reweetCountStr = String(count>0 ? "\(count)":"转发")
            
            let commentContStr = String(statusViewModel?.status?.comments_count ?? 0)
            
            let unlikeContStr = String(statusViewModel?.status?.attitudes_count ?? 0)
            
            if reweetCountStr == "0" {
                
                retweetButton?.setTitle("转发", for: .normal)
            } else {
                
                retweetButton?.setTitle(reweetCountStr, for: .normal)
            }
            
            if commentContStr == "0" {
                
                commentButton?.setTitle("评论", for: .normal)
            } else {
                
                commentButton?.setTitle(commentContStr, for: .normal)
            }
            
            if unlikeContStr == "0" {
                
                unlikeButton?.setTitle("赞", for: .normal)
                
            } else {
                
                unlikeButton?.setTitle(unlikeContStr, for: .normal)
            }
            
        }
        
        
        
    }
    
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        
        //转发
        retweetButton = addChildButton(imageName: "timeline_icon_retweet", title: "转发");
        
        //评论
        commentButton = addChildButton(imageName: "timeline_icon_comment", title: "评论")
        
        //点赞
        unlikeButton = addChildButton(imageName: "timeline_icon_unlike", title: "赞")
        
        retweetButton!.snp.makeConstraints { (make) in
            
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton!.snp.makeConstraints { (make) in
            
            make.left.equalTo(retweetButton!.snp.right)
            make.top.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
        }
        
        unlikeButton!.snp.makeConstraints { (make) in
            
            make.top.right.bottom.equalTo(self)
            make.left.equalTo(commentButton!.snp.right)
        }
        
        //创建两条竖线
        let firstLineView = addChildLineView()
        let secondLineView = addChildLineView()
        
        firstLineView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(retweetButton!.snp.right)
            make.centerY.equalTo(self)
        }
        
        secondLineView.snp.makeConstraints { (make) in
            
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentButton!.snp.right)
        }
        
    }
    
    //添加子按钮
    fileprivate func addChildButton(imageName: String,title: String) ->UIButton {
        
        let button = UIButton()
        
        //设置背景图片
        button.setBackgroundImage(UIImage(named:"timeline_card_bottom_background"), for: .normal)
        button.setImage(UIImage(named:imageName), for: .normal)
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        
        //添加子视图
        addSubview(button)
        
        return button
    }
    
    //添加子线视图
    fileprivate func addChildLineView() ->UIImageView {
        
        
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        //添加图片
        addSubview(imageView)
        
        return imageView
    }
    
    
}
